﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblNameMapDAL:ImtblNameMap
    {
        HKEA_CMSEntities model;
        mtblNameMap map;
        public mtblNameMapDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblNameMapDAL GetInstance()
        {
            return new mtblNameMapDAL();
        }
        public int InsertENT(mtblNameMap ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblNameMaps.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblNameMap> GetListByENT(mtblNameMap ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblNameMaps.Where(a => a.NameMapUID > 0);
            if (ent.NameMapUID > 0)
                result = result.Where(a => a.NameMapUID == ent.NameMapUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblNameMap> GetListByENT(mtblNameMap ent)
        {
            var result = model.mtblNameMaps.Where(a => a.NameMapUID > 0);
            if (ent.NameMapUID > 0)
                result = result.Where(a => a.NameMapUID == ent.NameMapUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblNameMap ent)
        {
            var result = model.mtblNameMaps.Where(a => a.NameMapUID > 0);
            if (ent.NameMapUID > 0)
                result = result.Where(a => a.NameMapUID == ent.NameMapUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblNameMap ent)
        {
            map = model.mtblNameMaps.Where(a=>a.NameMapUID==ent.NameMapUID).FirstOrDefault();
            if (map != null)
            {
                map.LastModifyBy = ent.LastModifyBy;
                map.LastModifyDate = DateTime.Now;
                map.NameMap_Display = ent.NameMap_Display;
                map.NameMap_Field = ent.NameMap_Field;
                map.NameMapType = ent.NameMapType;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblNameMap ent)
        {
            map = model.mtblNameMaps.Where(a => a.NameMapUID == ent.NameMapUID).FirstOrDefault();
            if (map != null)
            {
                model.mtblNameMaps.DeleteObject(map);
            }
            return model.SaveChanges();
        }
    }
}
